package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.exception.NotFoundException;
import com.vcc.data.entity.AdminParam;
import com.vcc.data.mapper.AdminParamMapper;
import com.vcc.service.boss.request.adminParam.BossAdminParamUpdateRequest;
import com.vcc.service.boss.response.adminParam.AdminParamVoBoss;
import com.vcc.service.service.AdminParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminParamServiceImpl extends ServiceImpl<AdminParamMapper, AdminParam> implements AdminParamService {
    @Override
    public Page<AdminParamVoBoss> paging(Integer pageNumber, Integer pageSize, String code, String name) {
        QueryChain<AdminParam> queryChain = queryChain();
        if (StringUtils.isNotBlank(code)) {
            queryChain.eq(AdminParam::getCode, code);
        }
        if (StringUtils.isNotBlank(name)) {
            queryChain.like(AdminParam::getName, name);
        }
        return pageAs(
                Page.of(
                        pageNumber != null ? pageNumber : 1,
                        pageSize != null ? pageSize : 20
                ),
                queryChain,
                AdminParamVoBoss.class
        );
    }

    @Override
    public Boolean update(BossAdminParamUpdateRequest bossAdminParamUpdateRequest) {
        AdminParam adminParam = getByIdOpt(bossAdminParamUpdateRequest.getId())
                .orElseThrow(() -> new NotFoundException("参数不存在"));
        UpdateChain<AdminParam> updatedChain = updateChain();
        if (StringUtils.isNotBlank(bossAdminParamUpdateRequest.getName())) {
            updatedChain.set(AdminParam::getName, bossAdminParamUpdateRequest.getName());
        }
        if (StringUtils.isNotBlank(bossAdminParamUpdateRequest.getValue())) {
            updatedChain.set(AdminParam::getValue, bossAdminParamUpdateRequest.getValue());
        }
        return updatedChain
                .eq(AdminParam::getId, adminParam.getId())
                .update();
    }

    @Override
    public Boolean existsByCode(String code) {
        return queryChain()
                .eq(AdminParam::getCode, code)
                .exists();
    }

    @Override
    public AdminParam getByCode(String code) {
        return queryChain()
                .eq(AdminParam::getCode, code)
                .oneOpt()
                .orElseThrow(() -> new NotFoundException("参数不存在"));
    }

    @Override
    public String getValueByCode(String code) {
        AdminParam adminParam = getByCode(code);
        return adminParam.getValue();
    }

    @Override
    public String getValueByCode(String code, String defaultValue) {
        try {
            AdminParam adminParam = getByCode(code);
            return adminParam.getValue();
        } catch (NotFoundException e) {
            return defaultValue;
        }
    }

    @Override
    public List<AdminParam> listByCodes(List<String> codes) {
        return queryChain()
                .in(AdminParam::getCode, codes)
                .list();
    }
}
